﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Editor
{
    partial class EditorWindow
    {
        private List<EditModes.IEditMode> m_EditModes = new List<EditModes.IEditMode>();
        private List<ModeToolbars.IModeToolbar> m_ModeToolbars = new List<ModeToolbars.IModeToolbar>();

        private EditModes.EditModeGeneral m_EditModeGeneral = new EditModes.EditModeGeneral();
        private EditModes.EditModeEnvironment m_EditModeEnvironment = new EditModes.EditModeEnvironment();

        private EditModes.IEditMode m_ActiveEditMode = null;
        private ModeToolbars.IModeToolbar m_ActiveModeToolbar = null;

        internal EditModes.IEditMode ActiveEditMode
        {
            get { return m_ActiveEditMode; }
        }

        private void InitializeEditModes()
        {
            m_EditModes.Clear();
            m_ModeToolbars.Clear();

            if (m_ActiveEditMode != null)
            {
                m_ActiveEditMode.ExitMode();
                m_ActiveEditMode = null;
            }

            if (m_ActiveModeToolbar != null)
            {
                m_ActiveModeToolbar.ExitMode();
                m_ActiveModeToolbar = null;
            }

            ctrl_ToolStripMode.SuspendLayout();
            
            foreach (TabPage tab in ctrl_ToolStripTabControl.TabControl.TabPages)
            {
                ModeToolbars.IModeToolbar modeToolbar = GetModeToolbarFromTab(tab);
                m_ModeToolbars.Add(modeToolbar);
                if (modeToolbar != null)
                    modeToolbar.ExitMode();

                EditModes.IEditMode editMode = GetEditModeFromTab(tab);
                m_EditModes.Add(editMode);
                if (editMode != null)
                    editMode.ExitMode();
            }

            ctrl_ToolStripMode.ResumeLayout(true);
        }

        private ModeToolbars.IModeToolbar GetModeToolbarFromTab(TabPage tab)
        {
            if (tab == ctrl_TabModesTabGeneral)
                return ctrl_ModeGeneral.ModeGeneral;
            else if (tab == ctrl_TabModesTabEnvironment)
                return ctrl_ModeEnvironment.ModeEnvironment;
            return null;
        }

        private EditModes.IEditMode GetEditModeFromTab(TabPage tab)
        {
            if (tab == ctrl_TabModesTabGeneral)
                return m_EditModeGeneral;
            else if (tab == ctrl_TabModesTabEnvironment)
                return m_EditModeEnvironment;
            return null;
        }

        private void OnActiveEditModeChanged(object sender, EventArgs e)
        {
            OnActiveEditModeChanged();
        }
        private void OnActiveEditModeChanged()
        {
            ctrl_ToolStripMode.SuspendLayout();

            if (m_ActiveModeToolbar != null)
                m_ActiveModeToolbar.ExitMode();
            if (m_ActiveEditMode != null)
                m_ActiveEditMode.ExitMode();

            ModeToolbars.IModeToolbar modeToolbar = m_ModeToolbars[ctrl_ToolStripTabControl.TabControl.SelectedIndex];
            m_ActiveModeToolbar = modeToolbar;
            EditModes.IEditMode editMode = m_EditModes[ctrl_ToolStripTabControl.TabControl.SelectedIndex];
            m_ActiveEditMode = editMode;

            if (m_ActiveEditMode != null)
                m_ActiveEditMode.EnterMode();
            if (m_ActiveModeToolbar != null)
                m_ActiveModeToolbar.EnterMode();

            ctrl_ToolStripMode.ResumeLayout(true);
        }
    }
}
